package com.bit.trans.controller;

import com.bit.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;

    @RequestMapping("/registryUser")
    public String registryUser(String userName, String password){
        //开启事务
        TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition);
        //执行SQL
        Integer re = userService.insertUser(userName, password);
        log.info("用户注册成功, 影响行数: "+ re);
        //提交事务
        dataSourceTransactionManager.commit(transaction);
        //回滚事务
//        dataSourceTransactionManager.rollback(transaction);
        return "用户注册成功";
    }
}
